package com.rr.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rr.entity.User;
import com.rr.dao.UserMapper;
import com.rr.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author zhuangcanrun
 * @Date 2021/11/29/23:11
 * @description
 **/

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public void function() {
        try {
            addUser1();
        } catch (Exception ignore) {
        }
    }

    @Transactional(rollbackFor = {IOException.class})
    @Override
    public void addUser1() throws IOException {
        User user = new User();
        user.setName("hehe");
        user.setAge(1);
        user.setEmail("haha@qq.com");
        this.baseMapper.insert(user);
        throw new IOException();
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    @Override
    public void addUser2() throws IOException {
        User user = new User();
        user.setName("hehe");
        user.setAge(1);
        user.setEmail("haha@qq.com");
        this.baseMapper.insert(user);
        throw new IOException();
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void addUser3() throws IOException {
        User user = new User();
        user.setName("hehe");
        user.setAge(1);
        user.setEmail("haha@qq.com");
        this.baseMapper.insert(user);
        throw new IOException();
    }

    @Override
    public IPage<User> selectPageVo(IPage<?> page, Integer age) {
        return baseMapper.selectPageVo(page, age);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addWithRequired(User user) {
        userMapper.insert(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addWithRequiredAndException(User user) {
        userMapper.insert(user);
        throw new RuntimeException();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addWithRequiredNew(User user) {
        userMapper.insert(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addWithRequiredNewAndException(User user) {
        userMapper.insert(user);
        throw new RuntimeException();
    }

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void addWithNested(User user) {
        userMapper.insert(user);
    }

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void addWithNestedAndException(User user) {
        userMapper.insert(user);
        throw new RuntimeException();
    }
}
